package com.my.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.entity.AttendanceLog;
import com.my.entity.GradeClass;
import com.my.service.AttendanceLogService;
import com.my.service.GradeClassService;
import com.my.util.Rest;
import com.my.util.execl.entity.AttendanceLogExecl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@RequestMapping("attendance")
public class AttendanceLogController {

    @Autowired
    private AttendanceLogService service;

    @Autowired
    private GradeClassService gcService;

    @GetMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") int pageNo, AttendanceLog log) {
        model.addAttribute("gradeList", gcService.list(new QueryWrapper<GradeClass>().eq("deep", 1)));
        model.addAttribute("uName", log.getUName());
        model.addAttribute("cuName", log.getCuName());
        model.addAttribute("gId", log.getGId());
        model.addAttribute("cId", log.getCId());
        model.addAttribute("status", log.getStatus());
        model.addAttribute("startTime", log.getStartTime());
        model.addAttribute("endTime", log.getEndTime());
        model.addAttribute("page", service.getList(new Page<>(pageNo, 10), log));
        return "attendance/list";
    }

    @GetMapping("exportExcel")
    public void exportExcel(AttendanceLog log, HttpServletResponse response) throws IOException {
        File file = null;
        try {
            file = File.createTempFile("学生考勤", ".xls");
        } catch (IOException e) {
            e.printStackTrace();

        }
        IPage<AttendanceLog> page = service.getList(new Page<>(1, 100000), log);
        List<AttendanceLogExecl> logExecls = new ArrayList<>();
        System.out.println(page.getRecords());
        page.getRecords().forEach(v -> {
            logExecls.add(new AttendanceLogExecl(v.getUName(), v.getWeekName(), v.getCuName(), v.getCuStartTime(), (v.getStatus() == 1 ? "上课" : (v.getStatus() == 2 ? "旷课" : (v.getStatus() == 3 ? "迟到" : "请假"))), v.getCreateTime()));
        });
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=download.xls");
        EasyExcel.write(file, AttendanceLogExecl.class).sheet().doWrite(logExecls);
        ServletOutputStream out = response.getOutputStream();
        out.write(FileReader.create(file).readBytes());
        file.delete();
        out.close();
    }

}
